﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Devdream.Web.Common;
using System.Drawing;
using Devdream.BLL;
using Devdream.Models;
using Devdream.Web.Areas.Member.Models;
using Microsoft.Owin;
using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
namespace Devdream.Web.Areas.Member.Controllers
{
    public class UserController : Controller
    {
        /// <summary>
        /// 认证管理器
        /// </summary>
        private IAuthenticationManager AuthenticationManager { get { return HttpContext.GetOwinContext().Authentication; } }
        //
        // GET: /Member/User/
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <returns></returns>
        public ActionResult Register()
        {
            return View();

        }

        /// <summary>
        /// 验证码
        /// </summary>
        /// <returns></returns>
        public ActionResult VerificationCode()
        {
            string verificationCode = Secrurity.CreateVerificationText(4);
            Bitmap img = Secrurity.CreateVerificationImage(verificationCode, 100, 30);
            img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            TempData["VerificationCode"] = verificationCode.ToLower();
            return null;

        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Register(RegisterViewModels register)
        {
            if (TempData["VerificationCode"] == null || TempData["VerificationCode"].ToString() != register.VerificationCode.ToLower())
            {
                ModelState.AddModelError("VerificationCode", "验证码不正确");
                return View(register);
            }
            if (ModelState.IsValid)
            {
                UserService userService = new UserService();
                if (userService.Exist(register.UserName)) ModelState.AddModelError("UserName", "用户名已存在");
                else
                {
                    User _user = new User()
                    {
                        UserName = register.UserName,
                        //默认用户组代码写这里
                        DisplayName = register.DisplayName,
                        Password = Secrurity.Sha256(register.Password),
                        //邮箱验证与邮箱唯一性问题
                        Email = register.Email,
                        //用户状态问题
                        Status = 0,
                        RegistrationTime = System.DateTime.Now,
                        LoginTime=System.DateTime.Now
                    };
                    _user = userService.Add(_user);
                    if (_user.UserID > 0)
                    {
                        return Content("注册成功！");
                        //AuthenticationManager.SignIn();
                    }
                    else { ModelState.AddModelError("", "注册失败！"); }
                }
            }
            return View(register);
        }
        public ActionResult Login(string returnUrl)
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(LoginViewModels loginModel)
        {
            if (ModelState.IsValid)
            {
                UserService userService = new UserService();
                var user = userService.Find(loginModel.UserName);
                if (user == null)
                    ModelState.AddModelError("UserName","用户名不存在");
                else if (user.Password == Common.Secrurity.Sha256(loginModel.PassWord))
                {
                    var identity = userService.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                    user.LoginTime = DateTime.Now;
                    user.LoginIP = Request.UserHostAddress;
                    userService.Update(user);
                    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = loginModel.RememberMe }, identity);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("PassWord", "密码错误");
                }

               
            }
            return View();
        }
        /// <summary>
        /// 注销用户登录
        /// </summary>
        /// <returns></returns>
        public ActionResult LoginOff()
        {
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
            return Redirect("~/");
        }
    }
}